from flask_login import UserMixin

from app import db
from sqlalchemy.sql import func
from werkzeug.security import generate_password_hash, check_password_hash


def datetime_format(value):
    if value:
        return value.strftime("%Y-%m-%d %H:%M:%S")
    else:
        return ''


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    nickname = db.Column(db.String(50), comment='昵称', default='新注册用户')
    username = db.Column(db.String(50), comment='用户名', nullable=False)
    email = db.Column(db.String(50), comment='邮箱', nullable=True)
    password = db.Column(db.String(50), comment='密码', nullable=True)
    oauth_type = db.Column(db.String(50), comment='OAuth 类型')
    oauth_uid = db.Column(db.String(250), comment='OAuth 登录后的唯一ID')
    create_time = db.Column(db.DateTime, default=func.now())
    update_time = db.Column(db.DateTime, default=None)
    is_delete = db.Column(db.Integer, default=0)

    def set_password(self, password):
        """ 设置密码, 对密码加密
        :param password:
        :return:
        """
        self.password = generate_password_hash(password)

    def validate_password(self, password):
        """ 密码校验
        :param password:
        :return:
        """
        return check_password_hash(self.password, password)


class WebsiteSort(db.Model):
    """网站分类表
    """
    # __tablename__ = 'website_sort'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), comment='分类名称', nullable=False)
    create_time = db.Column(db.DateTime, default=func.now())
    is_delete = db.Column(db.Integer, default=0)

    @property
    def website_sort_vo(self):
        return {
            'id': self.id,
            'name': self.name,
            'create_time': datetime_format(self.create_time)
        }


class Website(db.Model):
    """网站表
    网站只有一个分类, 但是可以有多个标签
    1. 时间默认当前时间:  default=func.now()
    2. 一对一关系 : relationship(uselist=False)
    """
    # __tablename__ = 'website'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(50), comment='名称', nullable=False)
    url = db.Column(db.String(250), comment='网址', nullable=False)
    remark = db.Column(db.String(250), comment='备注', default='')
    create_user = db.Column(db.String(100), comment='创建人', default='')
    order = db.Column(db.Integer, default=0, comment='排序')
    sort_id = db.Column(db.Integer, db.ForeignKey('website_sort.id'), comment='分类 id')
    sort = db.relationship('WebsiteSort', uselist=False, lazy=True)
    create_time = db.Column(db.DateTime, default=func.now())
    update_time = db.Column(db.DateTime, default=None)
    is_delete = db.Column(db.Integer, default=0)


class Dict(db.Model):
    """字典表
    """
    # __tablename__ = 'dict'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    parent_id = db.Column(db.Integer, nullable=False)
    name = db.Column(db.VARCHAR(50), comment='名称', nullable=False)
    value = db.Column(db.Integer, comment='值', default=None)
    dict_code = db.Column(db.Integer, comment='字典代码', default=None)

    create_time = db.Column(db.DateTime, default=func.now())
    update_time = db.Column(db.DateTime, default=None)
    is_delete = db.Column(db.Integer, default=0)


class Extract(db.Model):
    """摘录
    """
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.VARCHAR(50), comment='标题')
    content = db.Column(db.VARCHAR(500), comment='内容')
    type_id = db.Column(db.Integer, comment='类型')
    # type_id = db.Column(db.Integer, db.ForeignKey('dict.id'), comment='类型')
    # dict = db.relationship('Dict', uselist=False, lazy=True)
    create_time = db.Column(db.DateTime, default=func.now())
    is_delete = db.Column(db.Integer, default=0)

    @property
    def extract_vo(self):
        return {
            'id': self.id,
            'title': self.title,
            'content': self.content,
            'type_id': self.type_id
        }
